import { CommonModule } from '@angular/common';
import { Component, inject, OnInit } from '@angular/core';
import { FormGroup, NonNullableFormBuilder, ReactiveFormsModule, Validators } from '@angular/forms';
import { NzButtonModule } from 'ng-zorro-antd/button';
import { NzDividerModule } from 'ng-zorro-antd/divider';
import { NzFormModule } from 'ng-zorro-antd/form';
import { NzIconModule } from 'ng-zorro-antd/icon';
import { NzInputModule } from 'ng-zorro-antd/input';
import { NzModalModule } from 'ng-zorro-antd/modal';
import { NzRadioModule } from 'ng-zorro-antd/radio';
import { NzSelectModule } from 'ng-zorro-antd/select';
import { NzSpaceModule } from 'ng-zorro-antd/space';
import { NzTableModule } from 'ng-zorro-antd/table';
import { NzTreeSelectModule } from 'ng-zorro-antd/tree-select';
import { PostService } from './post.service';
import { PagerResponse, UnifyResponse } from '../../../../common/response';
import { NzMessageService } from 'ng-zorro-antd/message';


@Component({
  selector: 'app-post',
  imports: [
    NzInputModule,
    ReactiveFormsModule,
    NzButtonModule,
    NzFormModule,
    NzSelectModule,
    NzTableModule,
    NzDividerModule,
    NzIconModule,
    NzSpaceModule,
    CommonModule,
    NzTreeSelectModule,
    NzModalModule,
    NzRadioModule
  ],
  templateUrl: './post.component.html',
  styleUrl: './post.component.scss'
})
export class PostComponent implements OnInit {

  private readonly fb = inject(NonNullableFormBuilder)

  private readonly postSvc = inject(PostService);

  private readonly msg = inject(NzMessageService)

  postForm = this.fb.group({
    postCode: ['', [Validators.required]],
    postName: ['', [Validators.required]],
    postSort: ['', [Validators.required]],
    status: ['', [Validators.required]],
    remark: ['', [Validators.required]],
  })


  posts: any[] = [];
  loading: boolean = false;
  isVisible: boolean = false;
  title: string = '新增岗位';
  isEdit: boolean = false;
  post: any = {};


  loadPostList() {
    this.loading = true;
    this.postSvc.getPostList().subscribe({
      next: (res: PagerResponse<any>) => {
        this.posts = res.rows;
        this.loading = false;
      },
      error: () => {
        this.loading = false;
      }
    })
  }

  ngOnInit(): void {
    this.loadPostList()
  }

  // 新增岗位
  addPost() {
    this.isVisible = true;
    this.title = '新增岗位';
  }

  // 提交表单
  onSubmit() {
    if (this.postForm.valid) {
      this.postSvc.addPost({ ...this.postForm.getRawValue(), postSort: Number(this.postForm.get('postSort')?.value) }).subscribe({
        next: (res: UnifyResponse<any>) => {
          if (res.code === 200) {
            this.isVisible = false;
            this.msg.success(res.msg);
            this.postForm.reset();
            this.loadPostList();
          } else {
            this.msg.error(res.msg);
          }
        },
        error: () => {
          this.isVisible = false;
          this.msg.error('新增岗位失败');
          this.postForm.reset();
        }
      })
      console.log(this.postForm.value);
    } else {
      Object.values(this.postForm.controls).forEach(control => {
        if (control.invalid) {
          control.markAsDirty();
          control.updateValueAndValidity({ onlySelf: true });
        }
      })
    }
  }

  // 取消
  onCancel() {
    this.isVisible = false;
    this.postForm.reset();

  }

}
